home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Text / Word.php < prev   
PHP Script  |  2004-03-24  |  5KB  |  164 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩|
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩|
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,╩╩╩╩╩╩╩|
  9. // | that is bundled with this package in the file LICENSE, and is╩╩╩╩╩╩╩╩|
  10. // | available at through the world-wide-web at╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩|
  11. // | http://www.php.net/license/2_02.txt.╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩|
  12. // | If you did not receive a copy of the PHP license and are unable to╩╩╩|
  13. // | obtain it through the world-wide-web, please send a note to╩╩╩╩╩╩╩╩╩╩|
  14. // | license@php.net so we can mail you a copy immediately.╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩|
  15. // +----------------------------------------------------------------------+
  16. // | Author: George Schlossnagle <george@omniti.com>                      | 
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id$
  20.  
  21. /*
  22.  * Text_Word calculates the number of syllables in a word, based off of
  23.  * the number of contiguous vowel groupings in the word and applying
  24.  * matches to detect special cases.
  25.  
  26.  * require_once 'Text/Word.php'
  27.  * $word = new Text_Word('word');
  28.  * $word->numSyllables();  // returns 1
  29.  *
  30.  * @package Text_Word
  31.  * @author George Schlossnagle <george@omniti.com>
  32.  */
  33.  
  34.  
  35. class Text_Word {
  36.     /* The word 
  37.      *
  38.      * @var string
  39.      * @access public
  40.      */
  41.     var $word;
  42.  
  43.     /* The number of syllables.  This is internal, the value should be 
  44.      * accessed through the accessor.
  45.      *
  46.      * @var number
  47.      * @access protected
  48.      */
  49.     var $_numSyllables = 0;
  50.  
  51.     /* The special cases of fragments which detect as 1 but should be 2
  52.      * syllables.
  53.      *
  54.      * @var array
  55.      * @access static protected
  56.      */
  57.     var $doubleSyllables = array('/\wlien/', // alien but not lien
  58.                                  '/bl$/',   // syllable
  59.                                  '/io/',    // biography
  60.                                  );
  61.  
  62.     /* The special cases of fragments which detect as 2 but should be 1
  63.      * syllables.
  64.      *
  65.      * @var array
  66.      * @access static protected
  67.      */
  68.     var $silentSyllables = array('/\wely$/',    // absolutely but not ely
  69.                                  '/\wion/',
  70.                                  '/iou/',
  71.                                 );
  72.     
  73.     /*
  74.      * Constructs a word by name.
  75.      *
  76.      * @param  string
  77.      * @access public
  78.      */
  79.     function Text_Word($name = '') 
  80.     {
  81.         $this->word = $name;
  82.     }
  83.  
  84.     /*
  85.      * Helper function, canocalizes the word.
  86.      *
  87.      * @param  string
  88.      * @access protected
  89.      */
  90.     function _mungeWord($scratch) 
  91.     {
  92.         // conanonicalize the word
  93.         $scratch = strtolower($scratch);
  94.         // trailings e's are almost always silent in English
  95.         // so remove them
  96.         $scratch = preg_replace("/e$/", "", $scratch);
  97.         return $scratch;
  98.     }
  99.  
  100.     /*
  101.      * Helper function, counts syllable exceptions
  102.      *
  103.      * @param  string
  104.      * @access protected
  105.      */
  106.     function _countSpecialSyllables($scratch) 
  107.     {
  108.         $mod = 0;
  109.         // decrement our syllable count for words that contain
  110.         // 'silent' syllables, e.g. ely in 'abosultely'
  111.         foreach( $this->silentSyllables as $pat ) {
  112.             if(preg_match($pat, $scratch)) {
  113.                 $mod--;
  114.             }
  115.         }
  116.         // increment syllable count for certain conjoined vowel
  117.         // patterns which produce two syllables e.g.
  118.         // 'io' in 'biology'
  119.         foreach( $this->doubleSyllables as $pat ) {
  120.             if(preg_match($pat, $scratch)) {
  121.                 $mod++;
  122.             }
  123.         }
  124.         return $mod;
  125.     }
  126.  
  127.     /*
  128.      * Returns the number of syllables.  Caches the value in the object.
  129.      *
  130.      * @access public
  131.      */
  132.     function numSyllables() 
  133.     {
  134.         // cache the value in this object
  135.         if($this->_numSyllables) {
  136.             return $this->_numSyllables;
  137.         }
  138.         $scratch = $this->_mungeWord($this->word);
  139.         // Split the word on the vowels.  a e i o u, and for us always y
  140.         $fragments = preg_split("/[^aeiouy]+/", $scratch);
  141.  
  142.         // remove null elements at the head and tail of 
  143.         // $fragments
  144.         if(!$fragments[0]) {
  145.             array_shift($fragments);
  146.         }
  147.         if(!$fragments[count($fragments) - 1]) {
  148.             array_pop($fragments);
  149.         }
  150.  
  151.         // modify our syllable count for special cases
  152.         $this->_numSyllables += $this->_countSpecialSyllables($scratch);
  153.         // now count our syllable
  154.         if(count($fragments)) {
  155.             $this->_numSyllables += count($fragments);
  156.         }
  157.         else {
  158.             $this->_numSyllables = 1;
  159.         }
  160.         return $this->_numSyllables;
  161.     }
  162. }
  163. ?>
  164.